package de.se.ludoapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import de.se.ludoapp.LudoApplication;
import de.se.ludoapp.R;
import de.se.ludoapp.activity.task.AcceptRequestTask;
import de.se.ludoapp.activity.task.CheckRequestsTask;
import de.se.ludoapp.activity.task.IsGameStartedTask;
import de.se.ludoapp.activity.task.LeaveOpenGameTask;
import de.se.ludoapp.activity.task.NewGameTask;
import de.se.ludoapp.activity.task.StartGameTask;
import de.se.ludoapp.gaming.Game;
import de.se.ludoapp.gaming.PlayerType;

/**
 * Table opening View
 * 
 * @author Marlen Rotert
 * 
 */
public class NewGameActivity extends Activity {

	private LudoApplication ludoApp;

	private Handler handlerCheckRequests;

	private Runnable runnableCheckRequests;

	private Handler handlerIsGameStarted;

	private Runnable runnableIsGameStarted;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Initialise Stub LudoServer
		ludoApp = (LudoApplication) this.getApplication();

		// View
		setContentView(R.layout.new_game);

		Intent callingIntent = getIntent();
		Bundle bundle = callingIntent.getExtras();
		if (bundle != null) {
			// Call from Button OpenGameTable
			if ((Boolean) bundle.get("OpenNewGame")) {
				new NewGameTask(ludoApp, NewGameActivity.this).execute();
			}
		}

		// button for leaving table
		final Button buttonLeaveTable = (Button) this
				.findViewById(R.id.buttonNewGameTableLeave);
		buttonLeaveTable.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				buttonLeaveTable.setEnabled(false);
				handlerCheckRequests.removeCallbacks(runnableCheckRequests);
				handlerIsGameStarted.removeCallbacks(runnableIsGameStarted);
				new LeaveOpenGameTask(ludoApp, NewGameActivity.this).execute();
			}
		});

		// button for start game
		final Button buttonStartGame = (Button) this
				.findViewById(R.id.buttonNewGameStart);
		buttonStartGame.setVisibility(View.GONE);
		buttonStartGame.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				buttonStartGame.setEnabled(false);
				new StartGameTask(ludoApp, NewGameActivity.this).execute();
			}

		});

		// ListView for requests of other users to join game
		final ListView listViewPlayerRequests = (ListView) this
				.findViewById(R.id.listViewNewGamePlayerRequests);
		listViewPlayerRequests.setVisibility(View.GONE);
		listViewPlayerRequests
				.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// accept player
						listViewPlayerRequests.setEnabled(false);
						String acceptPlayer = (String) listViewPlayerRequests
								.getAdapter().getItem(position);
						new AcceptRequestTask(ludoApp, NewGameActivity.this,
								acceptPlayer).execute();
					}
				});

		updateView();

		// get all requests, delay 3 sek
		handlerCheckRequests = new Handler();
		runnableCheckRequests = new Runnable() {
			public void run() {
				new CheckRequestsTask(ludoApp, NewGameActivity.this).execute();
				handlerCheckRequests.postDelayed(this, 3000);
			}
		};
		handlerCheckRequests.postDelayed(runnableCheckRequests, 2000);

		// is game started, delay 3 sek
		handlerIsGameStarted = new Handler();
		runnableIsGameStarted = new Runnable() {
			public void run() {
				new IsGameStartedTask(ludoApp, NewGameActivity.this).execute();
				handlerIsGameStarted.postDelayed(this, 3000);
			}
		};
		handlerIsGameStarted.postDelayed(runnableIsGameStarted, 2000);

	}

	@Override
	protected void onPause() {
		super.onPause();
		handlerCheckRequests.removeCallbacks(runnableCheckRequests);
		handlerIsGameStarted.removeCallbacks(runnableIsGameStarted);
	}

	public void updateView() {
		Game game = ludoApp.getCurrentGame();
		if (game != null) {
			// Table
			TextView textViewGameId = (TextView) findViewById(R.id.textViewNewGameId);
			textViewGameId.setText(getResources().getString(
					R.string.textViewNewGameId)
					+ " " + game.getGameId());
			// Dungeon Master (Yellow)
			TextView textViewGamePlayerYellow = (TextView) findViewById(R.id.textViewNewGamePlayerYellow);
			textViewGamePlayerYellow.setText(getResources().getString(
					R.string.textViewNewGamePlayerYellow)
					+ " " + game.getNameYellowPlayer());
			// Player Red
			TextView textViewGamePlayerRed = (TextView) findViewById(R.id.textViewNewGamePlayerRed);
			textViewGamePlayerRed.setText(getResources().getString(
					R.string.textViewNewGamePlayerRed)
					+ " " + game.getNameRedPlayer());
			// Player Green
			TextView textViewGamePlayerGreen = (TextView) findViewById(R.id.textViewNewGamePlayerGreen);
			textViewGamePlayerGreen.setText(getResources().getString(
					R.string.textViewNewGamePlayerGreen)
					+ " " + game.getNameGreenPlayer());
			// Player Blue
			TextView textViewGamePlayerBlue = (TextView) findViewById(R.id.textViewNewGamePlayerBlue);
			textViewGamePlayerBlue.setText(getResources().getString(
					R.string.textViewNewGamePlayerBlue)
					+ " " + game.getNameBluePlayer());

			Button buttonStartGame = (Button) findViewById(R.id.buttonNewGameStart);
			ListView listViewPlayerRequests = (ListView) findViewById(R.id.listViewNewGamePlayerRequests);
			if (ludoApp.getPlayerTypeOfUser().equals(PlayerType.PLAYER_YELLOW)) {
				buttonStartGame.setVisibility(View.VISIBLE);
				listViewPlayerRequests.setVisibility(View.VISIBLE);
			} else {
				buttonStartGame.setVisibility(View.GONE);
				listViewPlayerRequests.setVisibility(View.GONE);
			}
		}

	}

	@Override
	public void onBackPressed() {
		// If you pushed backed button on your smartphone, it will be happen
		// nothing.
		// You have to leave the game (gameboard) active over a button in the
		// game.
	}

}
